#!/usr/bin/env bash
# change time zone
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
timedatectl set-timezone Asia/Shanghai

# set repo
rm -rf /etc/yum.repos.d/*
curl -Lo /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
curl -Lo /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
cat <<EOF | tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.32/rpm/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.32/rpm/repodata/repomd.xml.key
EOF


yum-config-manager --disable kubernetes
yum clean all
yum makecache

# install common software
yum install -y wget curl conntrack-tools vim net-tools telnet tcpdump bind-utils socat ntp lrzsz dos2unix git

# enable ntp to sync time
echo 'sync time'
systemctl start ntpd
systemctl enable ntpd

# disable security
systemctl stop firewalld
systemctl disable firewalld
systemctl stop iptables
systemctl disable iptables

# disable selinux
echo 'disable selinux'
setenforce 0
sed -i 's/=enforcing/=disabled/g' /etc/selinux/config

# iptables setup
echo 'iptables setup'
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sudo sysctl --system

# enable ipvs

# set hostname
echo 'set host name resolution'
cat >> /etc/hosts <<EOF
192.168.31.201 node01
192.168.31.202 node02
192.168.31.203 node03
EOF

cat /etc/hosts

# set nameserver
echo 'set nameserver'
echo "nameserver 114.114.114.114">/etc/resolv.conf
cat /etc/resolv.conf

# disable swap
echo 'disable swap'
swapoff -a
sed -i '/swap/s/^/#/' /etc/fstab

# install docker
echo 'install docker'
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo yum makecache fast
sudo yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
mkdir /etc/docker -p
cat > /etc/docker/daemon.json <<EOF
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "registry-mirrors" : ["https://e97bf3ff28c74b62ac99a5f949cd62ba.mirror.swr.myhuaweicloud.com"]
}
EOF
systemctl daemon-reload
systemctl enable docker
systemctl restart docker

# install cri-dockerd
echo "install cri-dockerd"
wget https://github.com/Mirantis/cri-dockerd/releases/download/v0.3.14/cri-dockerd-0.3.14-3.el7.x86_64.rpm
rpm -ivh cri-dockerd-0.3.14-3.el7.x86_64.rpm
sed -i '/ExecStart/s%$%--pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.9%' /usr/lib/systemd/system/cri-docker.service
systemctl daemon-reload
systemctl enable cri-docker
systemctl start cri-docker

# install kubeadm
echo "install kubelet kubeadm kubectl"
yum-config-manager --enable kubernetes
yum install -y kubelet kubeadm kubectl
systemctl enable kubelet && systemctl start kubelet

# add password for vagrant/root
sed -i '/PasswordAuthentication/c\PasswordAuthentication yes' /etc/ssh/sshd_config
sed -i '/PermitRootLogin/s/^#/#&/p' /etc/ssh/sshd_config
echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
sudo systemctl restart sshd
echo 123456|sudo passwd vagrant --stdin
echo 123456|sudo passwd root --stdin

# disable kubernetes repo
yum-config-manager --disable kubernetes